
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Text
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for special_settings.
	''' </summary>
	Public Partial Class special_settings
		Inherits BaseWebPage

		Protected dgSpeccialSettings As System.Web.UI.WebControls.DataGrid
		Protected spu As SpecialUser
		Protected spg As SpecialGroup
		Protected sRedirectPage As String = Constants.ACCOUNT_SETTINGS_PAGE
		Private grpID As Integer = -1
		Private grp As Group = Nothing
		Private isGroupMode As Boolean = False
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' this.pageTemplate.PageTitle = PageTitles.SPECIAL_SETTIINGS_PAGE;

			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.SPECIAL_SETTINGS, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			If Constants.SHOW_MUSIC <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			If Request.Params("userID") <> Nothing AndAlso Request.Params("userID") <> "" Then
				Dim id As Integer = Convert.ToInt32(Request.Params("userID"))

				If id <> usr.ID AndAlso usr.Status <> Constants.appAdmin Then
					Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
				End If

				If id <> usr.ID Then
					usr = Users.GetUserByID(id)
					sRedirectPage = Constants.MEMBERS + "?id=" + id
				End If
			End If

			dgSpecialSettings.Columns(1).HeaderText = Me.GetPageConstText(htPageConstants, "CONST_ColumnNamePage_TEXT")

			spu = New SpecialUser(usr.ID, usr.Name, usr.LastName)

			If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
				Try
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
				Catch
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End Try
				grp = Groups.GetGroupByID(grpID)
				If grp = Nothing Then
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End If

				isGroupMode = True
				spg = New SpecialGroup(grp.ID, grp.Name)
				hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
				hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)
			End If

			If Not IsPostBack Then
				BindGrid()
			End If
		End Sub

		Protected Function CreateDataSource() As ICollection
			Dim grpSpecialPages As ArrayList = Nothing
			Dim userSpecialPages As ArrayList = Nothing
			Dim tmpSpecialPage As SpecialUser.UserSpecialPage = Nothing
			Dim tmpGrpSpecialPage As SpecialGroup.GroupSpecialPage = Nothing
			Dim specialPagesCount As Integer = 0
			Dim i As Integer

			If isGroupMode Then
				grpSpecialPages = spg.LoadGroupSpecialPages()
				specialPagesCount = grpSpecialPages.Count
				tmpGrpSpecialPage = New SpecialGroup.GroupSpecialPage()
			Else
				userSpecialPages = spu.LoadUserSpecialPages()
				specialPagesCount = userSpecialPages.Count
				tmpSpecialPage = New SpecialUser.UserSpecialPage()
			End If

			Dim dt As New DataTable()

			If specialPagesCount > 0 Then
				Dim dr As DataRow
				dt.Columns.Add(New DataColumn("CheckValue", GetType(Integer)))
				dt.Columns.Add(New DataColumn("IsChecked", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("SpecialPageName", GetType(String)))

				' List special pages
				i = 0
				While i < specialPagesCount
					If isGroupMode Then
						tmpGrpSpecialPage = DirectCast(grpSpecialPages(i), SpecialGroup.GroupSpecialPage)
						dr = dt.NewRow()
						dr(0) = tmpGrpSpecialPage.spID
						dr(1) = tmpGrpSpecialPage.IsMember
						dr(2) = tmpGrpSpecialPage.Name
					Else
						tmpSpecialPage = DirectCast(userSpecialPages(i), SpecialUser.UserSpecialPage)
						dr = dt.NewRow()
						dr(0) = tmpSpecialPage.spID
						dr(1) = tmpSpecialPage.IsMember
						dr(2) = tmpSpecialPage.Name
					End If
					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While
			End If
			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindGrid()
			dgSpecialSettings.DataSource = CreateDataSource()
			dgSpecialSettings.DataBind()
		End Sub
		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim cbCheckedValue As String
			Dim cb As CheckBox

			For Each dgi As DataGridItem In Me.dgSpecialSettings.Items
				cb = DirectCast(dgi.Cells(0).Controls(1), CheckBox)
				cbCheckedValue = Request.Params(dgi.Cells(0).Controls(1).UniqueID)

				If cbCheckedValue <> Nothing AndAlso cbCheckedValue.Equals("on") Then
					If isGroupMode Then
						Dim tmpGrpSpecialPage As New SpecialGroup.GroupSpecialPage()
						tmpGrpSpecialPage.spID = Convert.ToInt32(cb.Attributes("value"))
						tmpGrpSpecialPage.IsMember = True
						spg.SpecialPages.Add(tmpGrpSpecialPage)
					Else
						Dim tmpSpecialPage As New SpecialUser.UserSpecialPage()
						tmpSpecialPage.spID = Convert.ToInt32(cb.Attributes("value"))
						tmpSpecialPage.IsMember = True
						spu.SpecialPages.Add(tmpSpecialPage)
					End If
				End If
			Next
			If isGroupMode Then
				spg.Save()
			Else
				spu.Save()
			End If

			dgSpecialSettings.DataBind()

			If isGroupMode Then
				Response.Redirect(Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID)
			Else
				Response.Redirect(sRedirectPage)
			End If
		End Sub

	End Class
End Namespace






